package com.jd.user.controller;

import com.jd.basic.exception.BusinessException;
import com.jd.basic.util.AjaxsResult;
import com.jd.user.dto.LoginDto;
import com.jd.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogininfoService logininfoService;
    @PostMapping("/account")
    public AjaxsResult emailRegister(@RequestBody LoginDto loginDto){
        try {
            // 用map集合接收两个信息，token登录，前端传来的参数
            Map<String,Object> map = logininfoService.accountLogin(loginDto);
            return new AjaxsResult().me().setResultObj(map);
            //只要有业务异常都要加
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxsResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxsResult(false,"登录失败");
        }
    }

    @PostMapping("/wechat")
    public AjaxsResult wechatLogin(@RequestBody LoginDto loginDto){
        try {

            return logininfoService.wechatLogin(loginDto.getCode());
            //只要有业务异常都要加
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxsResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxsResult(false,"登录失败");
        }
    }
    @PostMapping("/wechat/binder")
    public AjaxsResult wechatBinder(@RequestBody Map<String,String> map){
        try {

            return logininfoService.wechatBinder(map);
            //只要有业务异常都要加
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxsResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxsResult(false,"登录失败");
        }
    }
}
